// pages/mine/carList/carList.js
import config from "../../../config.js";
const base = config;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //车辆列表
    carList: [],
  },

  //删除车辆
  deleteCar: function(e) {
    var instance = this;
    wx.showModal({
      title: "温馨提示",
      content: "是否确认删除车辆信息？",
      showCancel: true,
      confirmColor: "#137AF0",
      success: function(er) {
        console.log(er);
        if (er.confirm) {
          let id = e.currentTarget.dataset.id,
            index = e.currentTarget.dataset.index,
            that = this;
          wx.showLoading({
            title: '正在删除',
          })
          wx.request({
            url: base.carDelete,
            header: {
              'content-type': 'application/x-www-form-urlencoded',
              'Cookie': wx.getStorageSync("session")
            },
            data: {
              id: id
            },
            success: function(res) {
              wx.hideLoading();
              console.log(res);
              if (res.data.status == 200) {
                wx.showToast({
                  title: '删除成功',
                })
                instance.getCarList();
              } else {
                wx.showToast({
                  title: "删除失败",
                  icon: "none"
                })
              }
            }
          })
        }
      }
    })
  },



  //违章查询
  illegalQuery: function(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../../peccancyList/peccancyList?id=' + id,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    //this.getCarList();
  },
  getCarList: function() {
    var that = this;
    //请求车辆列表
    wx.request({
      url: base.myCarsList,
      header: {
        'Cookie': wx.getStorageSync("session")
      },
      data: {
        start: 0
      },
      success: function(res) {
        console.log(res);
        if (res.statusCode == 200) {
          that.setData({
            carList: res.data.data
          })
        } else {
          wx.showToast({
            title: '车辆列表获取失败',
            icon: 'none'
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getCarList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  carDetailAction: function(e) {
    var carId = e.currentTarget.dataset.carid;
    wx.navigateTo({
      url: '../minecar/carDetail?id=' + carId,
    })
  },
  addAction: function() {
    wx.navigateTo({
      url: '../../addCar/addCar',
    })
  },
  setDefault: function(e) {
    console.log(e);
    var carId = e.detail.value;
    var that = this;

    //设置默认
    wx.request({
      url: base.defaultCar,
      header: {
        'Cookie': wx.getStorageSync("session")
      },
      data: {
        id: carId
      },
      success: function(res) {
        console.log(res);
        if (res.data.status == 200) {
          that.setData({
            carList: res.data.data
          })
        }
        wx.showToast({
          title: res.data.message,
          icon: 'none'
        })
      }
    })
  }
})